$(function() {
	if($("#state").val() == "1" || $("#resortid").val() == null || $("#resortid").val() == 0){
		$("#doVerify").css("display","none");
	}
	if($("#state").val() == "0" || $("#resortid").val() == null || $("#resortid").val() == 0){
		$("#cancelVerify").css("display","none");
	}

var initFlg = 1;
$("#selectResortArea0").combobox({
	url:appPath+'/goods/getCodesForCombox.action',
	valueField:'id',
	textField:'text',
	onSelect:function(classificationRecord){
		$("#hiddenAreaId0").val(classificationRecord.id);

		if(classificationRecord.text=="请选择"){
			$("#hiddenAreaId").val("");
			$("#hiddenAreaId2").val("");
			$("#selectResortAreaPro").combobox("clear");
			$("#selectResortArea2").combobox("clear");

//			$('#selectResortAreaPro').combobox('setValues', ["请选择"]);
//			$('#selectResortArea2').combobox('setValues', []);
			$('#selectResortAreaPro').combobox(
					{url:null, valueField:'id', textField:'text', data:[{'id':'','text':'请选择'}]});
			$('#selectResortArea2').combobox(
					{url:null, valueField:'id', textField:'text', data:[{'id':'','text':'请选择'}]});
			return;
		}
		
		$("#selectResortAreaPro").combobox({
			url:appPath+'/goods/getCodesForCombox.action?classification='+classificationRecord.id,
			valueField:'id',
			textField:'text',
			onSelect:function(record){
				$("#hiddenAreaId").val(record.id);

				if(record.text=="请选择"){
					$("#hiddenAreaId2").val("");
					$("#selectResortArea2").combobox("clear");

//					$('#selectResortArea2').combobox('setValues', []);
					$('#selectResortArea2').combobox(
							{url:null, valueField:'id', textField:'text', data:[{'id':'','text':'请选择'}]});
					return;
				}
				
				$("#selectResortArea2").combobox({
					url:appPath+'/goods/getCodesForCombox.action?parentid='+ record.id,
					valueField:'id',
					textField:'text',
					onSelect:function(record2){
						$("#hiddenAreaId2").val(record2.id);
					},
					onLoadSuccess:function(){
						if(initFlg == 1){
							initFlg = 0;
							if($("#hiddenAreaId2").val() != null && $("#hiddenAreaId2").val() !=""){
				    		
//							var areaName2 = $('#selectResortArea2').combobox('getData');  
//					          for (var i = 0; i < areaName2.length; i++) { 
//					        	  if ($("#hiddenAreaId2").val() == areaName2[i].id) {
//					        		  $("#selectResortArea2").combobox('select',$("#hiddenAreaId2").val());
//					        	  }
//					          }
							$("#selectResortArea2").combobox('select',$("#hiddenAreaId2").val());
							}
						} else {
				    		$("#selectResortArea2").combobox('select','');
				    	}
					}
	            });//.combobox('clear');
	        },
	        onLoadSuccess:function(){
	        	if(initFlg == 1 && $("#hiddenAreaId").val() != null && $("#hiddenAreaId").val() !=""){
	        		$("#selectResortAreaPro").combobox('select',$("#hiddenAreaId").val());
	        	} else {
	        		$("#selectResortAreaPro").combobox('select','');
	        	}
	        }
		});
	},
	onLoadSuccess:function(){
    	if(initFlg == 1 && $("#hiddenAreaId0").val() != null && $("#hiddenAreaId0").val() !=""){
    		$("#selectResortArea0").combobox('select',$("#hiddenAreaId0").val());
    	} else {
    		$("#selectResortArea0").combobox('select','');
    	}
    }
});
	
	
	
//	$('#selectResortAreaPro').change(function(){ 
//		alert();
//		$("#selectResortArea2").combobox({
//			url:appPath+'/goods/getCodesForCombox.action?parentid='+ $("#selectResortAreaPro").combobox("getValue"),
//			valueField:'id',
//			textField:'text'
//		});
//	});

	$("#selectResortArea").combobox({
		url:appPath+'/goods/getCodesForCombox.action',
		valueField:'id',
		textField:'text'
	});
	
	$("#selectResortLabels").combobox({
		url:appPath+'/goods/getCodesForComboxLabels.action?goodsClass='+$('#goodsClassHidden').val(),
		valueField:'id',
		textField:'text',
		multiple:true,
        onLoadSuccess:function(){
        	if($("#hiddenLabels").val() != null && $("#hiddenLabels").val() !=""){
        		$("#selectResortLabels").combobox('setValues',$("#hiddenLabels").val().split(','));
        	}
        }
	});
	
	$("#addArea").click(function() {
		$('#selectTypeDlg').dialog({
			buttons:[
				{text:'确定',iconCls:"icon-ok",handler:function(){
					var memberType=$("#selectResortArea").combobox("getValue");
					if(!memberType||memberType.length<=0){
						$.messager.alert('提示信息','请选择省！','warning');
						return;
					}
					doSaveArea();
					$('#selectTypeDlg').dialog('close');
				}},
				{text:'取消',iconCls:"icon-cancel",handler:function(){
					$('#selectTypeDlg').dialog('close');
				}}
			]
		});
		$('#selectTypeDlg').dialog('open');
	});
	
	/**
	 * 地域信息保存
	 */
	function doSaveArea(){
		var area=$("#selectResortArea").combobox("getValue");
		jqFrom =$("#areaForm");

		var type = $("#submitType").val();
		
		var url = appPath+'/goods/areaSave.action';
		
	    var sendData = jqFrom.serialize();    
		
		$.post(url,sendData,function(data){
			if(data=="success"){
				$.messager.alert('提示信息','区域信息保存成功。','info');
//				doQuery();
//				cancelPostData();
			}else if(data=="existId"){
				$.messager.alert('提示信息','市（区）已存在，请重新输入!','warning');
			}
		});
	}
	
	$("#addResortPic").click(function() {
		document.forms[0].action = appPath+"/webgoods/doResortPicAdd.action";
		document.forms[0].submit();
	});

	$("a[id^=delResortPic]").each(function(i) {
		$("#delResortPic" + i).click(function() {
		  $("#resPicDelIndex").val(i);
		  document.forms[0].action = appPath+"/webgoods/doResortPicDel.action";
		  document.forms[0].submit();
		});
	});
	
	$("#addAttractions").click(function() {
		document.forms[0].action = appPath+"/webgoods/doAttAdd.action";
		document.forms[0].submit();
	});

	$("a[id^=delAttractions]").each(function(i) {
		$("#delAttractions" + i).click(function() {
		  $("#attPicDelIndex").val(i);
		  document.forms[0].action = appPath+"/webgoods/doAttDel.action";
		  document.forms[0].submit();
		});
	});
	
	$("#doPostData").click(function(){
		var AreaId = $("#hiddenAreaId").val();
		var AreaId2 = $("#hiddenAreaId2").val();
		if(AreaId==null || AreaId==""){
			alert("请选择地址");
			return;
		}
		if(AreaId2==null || AreaId2==""){
			AreaId2 = AreaId;
		}
		
		$("#showMapFlg").val("");
		$("#showPreviewFlg").val("1");
		document.forms[0].action = appPath+"/webgoods/doResNew.action";
		document.forms[0].submit();
	});
	
	$("#doMapPreview").click(function(){
		$("#showMapFlg").val("1");
		$("#showPreviewFlg").val("");
		document.forms[0].action = appPath+"/webgoods/doResNew.action";
		document.forms[0].submit();
	});
	
	$("#canclePostData").click(function(){
		for(var i=0;i<$('.panel-tool-close',window.parent.document).length;i++){
			$('.panel-tool-close',window.parent.document).get(i).click(); 
		}	
	});
	

	
//	// 景区图标参照
//	var data = {};
//	var resIconUploader = new AjaxUpload('#resIconRefBtn', {
//		action : appPath + "/goods/fileAction.action",
//		name : 'file',
//		data : data,
//		autoSubmit : false,
//		bindInputFileName : 'resIconFileUrl',
//		onChange : function(file, extension) {
//			$("#resIconFileUrl").val(file);
//			$("#uploadBtn").removeAttr("disabled");
//		},
//		onSubmit : function(file, extension) {
//			data.fileFileName = file;
//		},
//
//		onComplete : function(file, response) {
//			alert(file);
//		}
//
//	});

//	// アップロード
//	$("#uploadBtn").click(function(){
//		uploader.submit();
//	});
//	
//	$("#uploadResIconRefBtnBtn").click(function(){
//		$("#uploadBtn").val();
//		resIconUploader.submit();
//	});
	
	//景区列表图
	$("#resIconRefFile").ajaxfileupload({
		'action' : appPath+"/goods/fileAction.action?goodsClass="+$('#goodsClassHidden').val(),//用于文件上传的服务器端请求地址  
		'params': {
	        'extra': 'info'
	      },
	      'onComplete': function(response) {
	    	  $("#iconFileUrl").val(JSON.stringify(response).replace(new RegExp('"','gm'),''));
	      },
	      'onStart': function() {
	        //if(weWantedTo) return false; // cancels upload
	      },
	      'onCancel': function() {

	      }
	    });
		
	$("input[id^=resPicRefFile]").each(function(i) {
		$("#resPicRefFile" + i).ajaxfileupload({
			'action' : appPath+"/goods/fileAction.action?goodsClass="+$('#goodsClassHidden').val(),//用于文件上传的服务器端请求地址  
			'params': {
		        'extra': 'info'
		      },
		      'onComplete': function(response) {
		    	  $("#picFileUrl" + i).val(JSON.stringify(response).replace(new RegExp('"','gm'),''));
		      },
		      'onStart': function() {
		        //if(weWantedTo) return false; // cancels upload
		      },
		      'onCancel': function() {

		      }
		    });
	});
	
	$("input[id^=attSoundFile]").each(function(i) {
		$("#attSoundFile" + i).ajaxfileupload({
			'action' : appPath+"/goods/fileAction.action?mp3=1&goodsClass="+$('#goodsClassHidden').val(),//用于文件上传的服务器端请求地址  
			'params': {
		        'extra': 'info'
		      },
		      'onComplete': function(response) {
		    	  $("#attSoundUrl" + i).val(JSON.stringify(response).replace(new RegExp('"','gm'),''));
		      },
		      'onStart': function() {
		        //if(weWantedTo) return false; // cancels upload
		      },
		      'onCancel': function() {

		      }
		    });
	});
	
	
	function applyAjaxFileUpload(element,fileInputId) {
		$(element).AjaxFileUpload({
			 url : appPath+"/goods/fileAction.action",//用于文件上传的服务器端请求地址  
		        secureuri : false,          //一般设置为false  
		        fileElementId : fileInputId,     //文件上传空间的id属性  <input type="file" id="resIconFile" name="file" />  
		        dataType : 'json',          //返回值类型 一般设置为json  
		        success : function(data, status) { 

		            //$("#downImg").show();   //待上传成功后 显示下载按钮  
		            //$("#downImg").attr("href","admin/downloadImage.do?filePath="+data.filePath);  
		            //$("#showImg").attr("src","admin/redImage.do?path=" + data.filePath);  
		        	$("#resIconFileUrl").val(data.fileFileName);
		        } 
			});
		}
	
	if($("#showPreviewFlg").val() == "1"){
		sFeatures = "dialogHeight:" + 600 + "px; dialogWidth:" + 367
				+ "px; status:no; resizable:no; help:no";
		$("#showPreviewFlg").val("");
		$("#showMapFlg").val("");
		return window.showModalDialog(appPath+"/webgoods/doPreview.action?id="+$("#id").val(), "", sFeatures);
	}
	if($("#showMapFlg").val() == "1"){
		sFeatures = "dialogHeight:" + 700 + "px; dialogWidth:" + 900
				+ "px; status:no; resizable:no; help:no";
		$("#showPreviewFlg").val("");
		$("#showMapFlg").val("");
		return window.showModalDialog(appPath+"/webgoods/mapPreview.action?id="+$("#id").val(), "", sFeatures);
	}
	
	//景区审核
	$("#doVerify").click(function(){
		var id=$("#resortid").val();
		if($("#state").val() == "1"){
			$.messager.alert('提示信息','只有未审核的景区才能进行审核！','warning');
			return;
		}
		else {
			var url = appPath+"/webgoods/goodsVerify.action?verifyId="+id;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','景区审核成功。','info');
					document.forms[0].action = appPath + "/webgoods/editGoodsInit.action?id="+ id;
					document.forms[0].submit();
				}
			});
		}
	});
	
	$("#cancelVerify").click(function(){
		var id=$("#resortid").val();
		if($("#state").val() == "0"){
			$.messager.alert('提示信息','只有审核通过的景区才能取消审核！','warning');
			return;
		}
		else {
			var url = appPath+"/webgoods/cancelVerify.action?verifyId="+id;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','审核已取消。','info');
					document.forms[0].action = appPath + "/webgoods/editGoodsInit.action?id="+ id;
					document.forms[0].submit();
				}
			});
		}
	});
	
	$("#doGoodsDel").click(function(){
		var id=$("#resortid").val();
		$.messager.confirm("确认", "是否确认删除", function (r) {  
	        if (r) {  
	        	var url = appPath+"/webgoods/delGoods.action?delIds="+id;
	    		$.post(url,function(data){
	    			if(data=="ok"){
	    				//$.messager.alert('提示信息','删除成功。','info');
	    				for(var i=0;i<$('.panel-tool-close',window.parent.document).length;i++){
	    					$('.panel-tool-close',window.parent.document).get(i).click(); 
	    				}
	    				window.parent.document.forms[0].submit();
	    			}
	    		}); 
	        }  
	    });  
	    return false; 
	});
});

function fileUpload() {
    $.ajaxFileUpload({
        url : appPath+"/goods/fileAction.action",//用于文件上传的服务器端请求地址  
        secureuri : false,          //一般设置为false  
        fileElementId : 'resIconFile',     //文件上传空间的id属性  <input type="file" id="resIconFile" name="file" />  
        dataType : 'json',          //返回值类型 一般设置为json  
        success : function(data, status) { 
            //$("#downImg").show();   //待上传成功后 显示下载按钮  
            //$("#downImg").attr("href","admin/downloadImage.do?filePath="+data.filePath);  
            //$("#showImg").attr("src","admin/redImage.do?path=" + data.filePath);  
        	$("#resIconFileUrl").val(data.fileFileName);
        }  
    }); 
  }  